// pages/cost/result/index.js
const app = getApp();
import api from '../../../../services/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonBounding: app.globalData.buttonBounding,
    type: '',
    qd: '',
    qdName: '',
    productId: 0,
    productName: '',
    seriesName: '',
    carSeriesId: '',
    carModel: '',
    priceInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    //url: `/pages/index/cost/result/index?type=${type}&qd=${qd}&productId=${productId}&productName=${productName}&seriesName=${seriesName}&carSeriesId=${carSeries.id}&carModel=${carSeries.seriesName}`
    if (e.type) {
      let qdName= '零售';
      if (e.qd === 'ds') {
        qdName = '电商'
      } else if (e.qd === 'pdi') {
        qdName = 'PDI'
      }
      console.log('e.productId', e.productId)
      this.setData({
        type: e.type,
        qd: e.qd,
        qdName: qdName,
        productId: e.productId,
        productName: e.productName,
        seriesName: e.seriesName,
        carSeriesId: e.carSeriesId,
        carModel: e.carModel
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.queryPriceInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goBack() {
    wx.navigateBack();
  },
  selectItem (e) {
    console.log(e)
    let itemName = e.currentTarget.dataset.itemname
    let priceInfo = this.data.priceInfo
    priceInfo[itemName + 'Active'] = !priceInfo[itemName + 'Active']
    
    if (itemName === 'price' || itemName === 'zyPrice') {
      if (itemName === 'price') {
        priceInfo.zyPriceActive = false
      } else {
        priceInfo.priceActive = false
      }
      priceInfo.qbxgPriceActive = false
      priceInfo.hbxgPriceActive = false

      priceInfo.zqmPriceActive = false
      priceInfo.yqmPriceActive = false
      priceInfo.zhmPriceActive = false
      priceInfo.yhmPriceActive = false

      priceInfo.zqyzbPriceActive = false
      priceInfo.yqyzbPriceActive = false
      priceInfo.zhyzbPriceActive = false
      priceInfo.yhyzbPriceActive = false

      priceInfo.cdPriceActive = false
      priceInfo.zcqPriceActive = false
      priceInfo.ycqPriceActive = false

      priceInfo.yqgPriceActive = false
      priceInfo.wxgPriceActive = false

      priceInfo.zhsjPriceActive = false
      priceInfo.yhsjPriceActive = false
    } else {
      priceInfo.zyPriceActive = false
      priceInfo.priceActive = false
    }
    let total = 0
    if(priceInfo.zyPriceActive){total+=priceInfo.zyPrice}
    if(priceInfo.priceActive){total+=priceInfo.price}
    if(priceInfo.qbxgPriceActive){total+=priceInfo.qbxgPrice}
    if(priceInfo.hbxgPriceActive){total+=priceInfo.hbxgPrice}
    if(priceInfo.zqmPriceActive){total+=priceInfo.zqmPrice}
    if(priceInfo.yqmPriceActive){total+=priceInfo.yqmPrice}
    if(priceInfo.zhmPriceActive){total+=priceInfo.zhmPrice}
    if(priceInfo.yhmPriceActive){total+=priceInfo.yhmPrice}
    if(priceInfo.zqyzbPriceActive){total+=priceInfo.zqyzbPrice}
    if(priceInfo.yqyzbPriceActive){total+=priceInfo.yqyzbPrice}
    if(priceInfo.zhyzbPriceActive){total+=priceInfo.zhyzbPrice}
    if(priceInfo.yhyzbPriceActive){total+=priceInfo.yhyzbPrice}
    if(priceInfo.cdPriceActive){total+=priceInfo.cdPrice}
    if(priceInfo.zcqPriceActive){total+=priceInfo.zcqPrice}
    if(priceInfo.ycqPriceActive){total+=priceInfo.ycqPrice}
    if(priceInfo.yqgPriceActive){total+=priceInfo.yqgPrice}
    if(priceInfo.wxgPriceActive){total+=priceInfo.wxgPrice}
    if(priceInfo.zhsjPriceActive){total+=priceInfo.zhsjPrice}
    if(priceInfo.yhsjPriceActive){total+=priceInfo.yhsjPrice}
    priceInfo.total = total
    console.log(priceInfo)
    this.setData({
      priceInfo
    });
  },
  async queryPriceInfo() {
    const { type, qd, qdName, productId, productName, seriesName, carSeriesId, carModel } = this.data;
    let params = {};
    if (carSeriesId) {
      params.carSeriesId = carSeriesId
      params.productId = productId
    } else {
      params.productId = productId
    }
    const res = await api.quote.queryPriceInfo(params);
    if (res.status == 200) {
      const introduceObj = JSON.parse(res.data.introduce);
      res.data.introduceObjLeft = [];
      res.data.introduceObjRight = [];
      for (let i = 0; i < introduceObj.length; i ++) {
        if (i % 2 === 0) {
          res.data.introduceObjLeft.push(introduceObj[i]);
        } else {
          res.data.introduceObjRight.push(introduceObj[i]);
        }
      }
      let priceInfo = res.data
      priceInfo.total = res.data.price + 0
      priceInfo.price = res.data.price
      priceInfo.zyPrice = res.data.price + 1000
      
      priceInfo.qbxgPrice = res.data.qbxgPrice
      priceInfo.hbxgPrice = res.data.hbxgPrice

      priceInfo.zqmPrice = res.data.dmPrice
      priceInfo.yqmPrice = res.data.dmPrice
      priceInfo.zhmPrice = res.data.dmPrice
      priceInfo.yhmPrice = res.data.dmPrice

      priceInfo.zqyzbPrice = res.data.qyzbPrice
      priceInfo.yqyzbPrice = res.data.qyzbPrice
      priceInfo.zhyzbPrice = res.data.hyzbPrice
      priceInfo.yhyzbPrice = res.data.hyzbPrice

      priceInfo.cdPrice = res.data.cdPrice
      priceInfo.zcqPrice = res.data.cqPrice
      priceInfo.ycqPrice = res.data.cqPrice

      priceInfo.yqgPrice = res.data.yqgPrice
      priceInfo.wxgPrice = res.data.hgbPrice

      priceInfo.zhsjPrice = res.data.whsjPrice
      priceInfo.yhsjPrice = res.data.whsjPrice

      priceInfo.priceActive = true
      priceInfo.zyPriceActive = false

      priceInfo.qbxgPriceActive = false
      priceInfo.hbxgPriceActive = false

      priceInfo.zqmPriceActive = false
      priceInfo.yqmPriceActive = false
      priceInfo.zhmPriceActive = false
      priceInfo.yhmPriceActive = false

      priceInfo.zqyzbPriceActive = false
      priceInfo.yqyzbPriceActive = false
      priceInfo.zhyzbPriceActive = false
      priceInfo.yhyzbPriceActive = false

      priceInfo.cdPriceActive = false
      priceInfo.zcqPriceActive = false
      priceInfo.ycqPriceActive = false

      priceInfo.yqgPriceActive = false
      priceInfo.wxgPriceActive = false

      priceInfo.zhsjPriceActive = false
      priceInfo.yhsjPriceActive = false
      this.setData({priceInfo});
    }
  },
})